package com.nj.dms.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.nj.dms.dao.FileDao;
import com.nj.dms.dao.impl.FileDaoImpl;

/**
 * 删除多个文件
 */
@WebServlet("/DeleteAllFile")
public class DeleteAllFile extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public DeleteAllFile() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String[] ids = request.getParameterValues("delID");

		FileDao fileDao = new FileDaoImpl();

		// 获取要删除文件的路径

		List<String> pathList = fileDao.getFilePath(ids);

		// 删除文件数据
		fileDao.deleteFile(ids);

		for (String path : pathList) {

			// 获取服务器真实路径
			String serPath = request.getServletContext().getRealPath("/") + "uploads" + File.separator + path;

			// 同时删除文件资源
			File file = new File(serPath);

			System.out.println("文件路径：" + serPath);

			if (file.exists()) {
				// 文件存在，即删除文件

				System.out.println("文件存在");

				file.delete();

			} else {

				System.out.println("文件不存在");

			}

		}

		// 返回到文件列表
		response.sendRedirect("MyFileList");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
